package ru.st.selenium.applogic2;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import ru.st.selenium.applogic.FilmHelper;
import ru.st.selenium.model.Film;
import ru.st.selenium.pages.AddFilmPage;

import static org.openqa.selenium.support.ui.ExpectedConditions.alertIsPresent;

public class FilmHelper2 extends DriverBasedHelper implements FilmHelper {

    public FilmHelper2(ApplicationManager2 manager) {
        super(manager.getWebDriver());
    }

    @Override
    public void create(Film film, boolean expectation) {
        pages.internalPage.clickAddFilmLink().ensurePageLoaded();

        pages.addFilmPage.ensurePageLoaded().setImdbField(film.getImdb())
                .setNameField(film.getTitle())
                .setYearField(film.getYear())
                .clickSubmitButton();
        if (expectation) {
            pages.addFilmPage.ensureFilmAdded();
        } else {
            pages.addFilmPage.ensureFilmNotAdded();
        }

    }

    @Override
    public void delete(Film film) {
        // TODO Auto-generated method stub
//        pages.homePage.gotoFilm();
    }

    public void delete(String title) {

        pages.homePage.gotoFilm(title);
        pages.viewFilmPage.ensurePageLoaded();
        pages.viewFilmPage.clickRemoveButton();
        wait.until(alertIsPresent()).accept();
    }

    @Override
    public List<Film> search(String title) {


        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String[] search2(String title){
        pages.homePage.doSearch(title);
        try {
            Thread.sleep(2000);//подождем пока загрузятся
        } catch (InterruptedException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        List<WebElement> webElements = driver.findElements(By.xpath("//div[@id='results']/a/div/div[2]"));
        String[] result = new String[webElements.size()];

        for (int i = 0; i < webElements.size(); i++) {
            result[i] = webElements.get(i).getText();
        }
        return result;
    }

}
